#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <string>
#include <ctime>
#include "Thread.hpp"
#include "lockguard.hpp"

using namespace std;

int tickets = 1000;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;


void threadRutine(void* args)
{
    string message = (const char*) args;  

    
    while(true)
    {
        LockGuard lockguard(&mutex);
        if(tickets > 0)
        {
            usleep(2000);
            cout << message << " get a ticket  " << "tickets:" << tickets-- << endl;
        }
        else
        {
            break;
        }

        usleep(12);
    }
}



int main()
{
    Thread t1(1,threadRutine,(void*)"hello1");
    Thread t2(2,threadRutine,(void*)"hello2");
    Thread t3(3,threadRutine,(void*)"hello3");
    Thread t4(4,threadRutine,(void*)"hello4");

    t1.run();
    t2.run();
    t3.run();
    t4.run();

    t1.join();
    t2.join();
    t3.join();
    t4.join();
    
    return 0;
}